package com.ibis.debuglistwidget;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

/**
 * This is the service that provides the factory to be bound to the collection
 * service.
 */
public class LogWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

/**
 * This is the factory that will provide data to the collection widget.
 */
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

    private static final String TAG = LogWidgetService.class.getSimpleName();
    private Context mContext;
    private Cursor mCursor;
    private Prefs mPrefs;
    private SharedPreferences sharedPreferences;

    public StackRemoteViewsFactory(Context context, Intent intent) {
        Log.d(TAG, "StackRemoteViewsFactory");
        mContext = context;
        intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        mPrefs = null;
    }

    public void onCreate() {
        // Since we reload the cursor in onDataSetChanged() which gets called
        // immediately after
        // onCreate(), we do nothing here.
    }

    public void onDestroy() {
        if (mCursor != null) {
            mCursor.close();
        }
    }

    public int getCount() {
        return mCursor.getCount();
    }

    public RemoteViews getViewAt(int position) {
        // Get the data for this position from the content provider
        // Log.d(TAG, "getViewAt" + position);

        sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(mContext);

        mPrefs = new Prefs(mContext);

//        Buffer mBuffer = mPrefs.getBuffer();

        int itemId;
        String text = "No log found";
        int color = 0;

        if (mCursor.moveToPosition(position)) {
            final int logColIndex = mCursor
                    .getColumnIndex(LogDataProvider.Columns.TEXT);
            text = mCursor.getString(logColIndex);
            final int tempColIndex = mCursor
             .getColumnIndex(LogDataProvider.Columns.COLOR);
             color = mCursor.getInt(tempColIndex);
        }

        // Return a proper item with the proper city and temperature. Just for
        // fun, we alternate
        // the items to make the list easier to read.
        // final String formatStr = mContext.getResources().getString(
        // R.string.item_format_string);

        itemId = (position % 2 == 0 ? R.layout.light_widget_item
                : R.layout.dark_widget_item);

        RemoteViews rv = new RemoteViews(mContext.getPackageName(), itemId);

        rv.setTextViewText(R.id.widget_item, text);
        rv.setFloat(R.id.widget_item, "setTextSize", mPrefs.getTextsize()
                .getValue());
        rv.setTextColor(R.id.widget_item, color);

        // String.format(formatStr, temp, log));

        // Set the click intent so that we can handle it and show a toast
        // message
        final Intent fillInIntent = new Intent();
//        Uri uri = Uri.withAppendedPath(LogWidgetProvider.CONTENT_URI, id);
//        fillInIntent.setData(uri);
        final Bundle extras = new Bundle();
        extras.putString(LogWidgetProvider.EXTRA_LOG_ID, text);
        fillInIntent.putExtras(extras);
        rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);

        return rv;
    }

    public RemoteViews getLoadingView() {
        // We aren't going to return a default loading view in this sample
        return null;
    }

    public int getViewTypeCount() {
        // Technically, we have two types of views (the dark and light
        // background views)
        return 2;
    }

    public long getItemId(int position) {
        return position;
    }

    public boolean hasStableIds() {
        return true;
    }

    public void onDataSetChanged() {
        // Refresh the cursor
        if (mCursor != null) {
            mCursor.close();
        }
        Log.d(TAG, "onDataSetChanged|start");
        mCursor = mContext.getContentResolver().query(
                LogDataProvider.CONTENT_URI, null, null, null, null);
        Log.d(TAG, "onDataSetChanged|end");
    }
}